<?php


/**
 * 下午茶申请单查询
 * @package  ajax
 * @subpackage  quanliadmin
 * @author   allen.qiang  <allen.qiang@fistforward.com> 2015-01-14 allen.qiang FUN-AC-0028-下午茶项目
 */

quanli_require('module/base/system/sysApply.class.php');

class ajax_applySearch {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {                
                case 1://查询下午茶申请单列表
                	$this->applyList();
                	break;
                case 2://新增下午茶申请单
                	$this->addApply();
                	break;
                case 3://删除下午茶申请单
                    $this->delApply();
                    break;
                case 4://修改下午茶申请单状态
                     $this->modApply();
                     break;
                case 5://入库
                    $this->applyWarehousing();
                    break;
                default:
                    $this->toReturn('参数错误');
                    break;

            }

        } else {

            $this->toReturn('参数错误');

        }

    }

    /**
     * 返回数据公共方法
     * @param string $message 提示信息
     * @param int $code 代码
     * @param mixed $data 其他数据
     */
    private function toReturn( $message, $code = 400, $data = null) {

        if (is_null($data)) {
            die( json_encode( array( 'code' => $code, 'message' => $message ) ) );
        } else {
            die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );
        }

    }

    //下午茶申请单显示页面
    public function applyList() {

        global $G_PGLOB;

        //实例下午茶采购单类
        $sysSearch = new sysApply();
        $sys_info_array = $sysSearch->getApplyList( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );


        if ($sys_info_array) {
            foreach($sys_info_array as $item) {

                $paid_amount=0;
                $search_sql = "SELECT SUM(paid_amount) AS paid_amount FROM `dunch_stock_batch` WHERE apply_id='".$item['apply_id']."'";
                $temp_item_array = &$G_PGLOB->DBCA->stmt_query($search_sql);
                if ($temp_item_array)
                    $paid_amount=$temp_item_array[0]->paid_amount;

                $status=$item['status'];
                $statusName=$this->getStatusName($status); //状态名称

                if($status==0){
                    $operatorString='<a href="javascript: void(0);" onclick="coupon.shengchan('.$item['apply_id'].');">生产</a>&nbsp;&nbsp;&nbsp;&nbsp;';
                    $operatorString.='<a href="javascript: void(0);" onclick="coupon.quxiao('.$item['apply_id'].');">取消</a>';
                }else if($status==1){
                    $operatorString='<a href="javascript: void(0);" onclick="coupon.shangche('.$item['apply_id'].');">上车</a>';
                }else if($status==2 || $status==3){
                    $operatorString='<a href="javascript: void(0);" onclick="coupon.ruku('.$item['apply_id'].');">入库</a>';
                }else{
                    $operatorString="";
                }

                $result_array['rows'][] = array(
                    'id' => $item['apply_id'],
                    'cell'=> array(
                        $operatorString, //操作
                        $item['apply_id'], //申请编号
                        $statusName, //状态
                        $item['apply_date'], //配送日期
                        $item['distribution_name'], //配送站
                        $item['createTime'], //申请时间
                        $item['apply_user_name'], //申请人
                        $item['total_num'], //申请数量
                        $paid_amount,//入库数量
                        $item['product_detail'], //商品详情
                        $sysSearch->get_dunch_stock_batch( $item['apply_id'] ),
                        $item['remark'] //备注
                    )
                );
            }
        }

        echo json_encode($result_array);

    }

    //添加采购单
    public function addApply(){
        //实例下午茶采购单类
        $sysApply = new sysApply();
        $sysApply_array = $sysApply->addApply();
        returnJsonData($sysApply_array['message'], $sysApply_array['code']);
    }

    //删除下午茶采购单
    public function delApply(){
        //实例下午茶采购单类
        $sysApply = new sysApply();
        $sysApply_array = $sysApply->delApply();
        returnJsonData($sysApply_array['message'], $sysApply_array['code']);
    }

    //修改下午茶采购单状态
    public function modApply(){
        //实例下午茶采购单类
        $sysApply = new sysApply();
        $sysApply_array = $sysApply->modApply();
        returnJsonData($sysApply_array['message'], $sysApply_array['code']);
    }

    //入库
    public function applyWarehousing(){
        //实例下午茶采购单类
        $sysApply = new sysApply();
        $sysApply_array = $sysApply->applyWarehousing();
        returnJsonData($sysApply_array['message'], $sysApply_array['code']);
    }

    //根据状态获取名称
    public function getStatusName($status){
        switch($status) {
            case 0:
               return "已提交";
               break;
            case 1:
                return "已生产";
                break;
            case 2:
                return "已上车";
                break;
            case 3:
                return "入库";
                break;
            case 10:
                return "已取消";
                break;
            default:
                return "";
                break;

        }
    }
  	
}